package ywpt.web.action.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;

import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.ClientDevice;
import ywpt.service.client.ClientDeviceService;
import ywpt.service.client.ClientKeshiService;
import ywpt.service.client.ClientService;
import ywpt.web.common.CommonDictionary;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ClientDeviceAction extends BaseAction implements
ModelDriven<ClientDevice>, Preparable{

	/**
	 * 客户设备Action
	 */
	private static final long serialVersionUID = 8736719679948847200L;
	private static final int DEFAULT_PAGE_SIZE = 20;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];//λȨ�����飬
	private Long id;
	private ClientDevice clientDevice;
	private List<ClientDevice> clientDeviceList = new ArrayList<ClientDevice>();
	private ClientDeviceService  clientDeviceService;
	
	private Long typeId;
	private String typeName;
	
	private Long clientId;
	private String clientName;
	
	private Long keshiId;
	private String keshiName;
	
	private Long providerId;
	private String providerName;

	private ClientService clientService;
	private ClientKeshiService clientKeshiService;

	//��ѯ���
 	private String qCode;
 	private String qName;
 	private String qType;
 	private String qClient;
 	private String qProvider;
 	private String qServicer;
 	private Date qStart;
 	private Date qEnd;
	
 

	public String getQCode() {
		return qCode;
	}

	public void setQCode(String code) {
		qCode = code;
	}

	public String getQName() {
		return qName;
	}

	public void setQName(String name) {
		qName = name;
	}

	public String getQType() {
		return qType;
	}

	public void setQType(String type) {
		qType = type;
	}

	public String getQClient() {
		return qClient;
	}

	public void setQClient(String client) {
		qClient = client;
	}

	public String getQProvider() {
		return qProvider;
	}

	public void setQProvider(String provider) {
		qProvider = provider;
	}

	public String getQServicer() {
		return qServicer;
	}

	public void setQServicer(String servicer) {
		qServicer = servicer;
	}

	public Date getQStart() {
		return qStart;
	}

	public void setQStart(Date start) {
		qStart = start;
	}

	public Date getQEnd() {
		return qEnd;
	}

	public void setQEnd(Date end) {
		qEnd = end;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ClientDevice getClientDevice() {
		return clientDevice;
	}

	public void setClientDevice(ClientDevice clientDevice) {
		this.clientDevice = clientDevice;
	}

	public List<ClientDevice> getClientDeviceList() {
		return clientDeviceList;
	}

	public void setClientDeviceList(List<ClientDevice> clientDeviceList) {
		this.clientDeviceList = clientDeviceList;
	}

	public ClientDeviceService getClientDeviceService() {
		return clientDeviceService;
	}

	public void setClientDeviceService(ClientDeviceService clientDeviceService) {
		this.clientDeviceService = clientDeviceService;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}

	public String getClientName() {
		return clientName;
	}

	public void setClientName(String clientName) {
		this.clientName = clientName;
	}

	public Long getKeshiId() {
		return keshiId;
	}

	public void setKeshiId(Long keshiId) {
		this.keshiId = keshiId;
	}

	public String getKeshiName() {
		return keshiName;
	}

	public void setKeshiName(String keshiName) {
		this.keshiName = keshiName;
	}

	public Long getProviderId() {
		return providerId;
	}

	public void setProviderId(Long providerId) {
		this.providerId = providerId;
	}

	public String getProviderName() {
		return providerName;
	}

	public void setProviderName(String providerName) {
		this.providerName = providerName;
	}
	public boolean[] getPopedom() {
		return popedom;
	}

	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
//****************************************************
	@Override
	public ClientDevice getModel() {
		return this.clientDevice;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.clientDevice = this.clientDeviceService.getClientDeviceById(id);
		}else{
			this.clientDevice = new ClientDevice();
		}
		HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.CONFIG_HARDWAER);
	}
	protected void showInfo() {
		if(clientDevice.getType() != null){
			this.setTypeId(clientDevice.getType().getId());
			this.setTypeName(clientDevice.getType().getType());
		}
		if(clientDevice.getClient() != null){
			this.setClientId(clientDevice.getClient().getId());
			this.setClientName(clientDevice.getClient().getClientName());
		}
		if(clientDevice.getKeshi() != null){
			this.setKeshiId(clientDevice.getKeshi().getId());
			this.setKeshiName(clientDevice.getKeshi().getName());
		}
		if(clientDevice.getProvider() != null){
			this.setProviderId(clientDevice.getProvider().getId());
			this.setProviderName(clientDevice.getProvider().getName());
		}
		if (clientDevice.getJobs().size()>0) {
				@SuppressWarnings("unused")		
				Limit limit = RequestUtils.getLimit(getRequest(), "ec", clientDevice.getJobs().size(), DEFAULT_PAGE_SIZE);
		}
	}
	// **************************��ݽ�������*******************************	
	public String list() {
		clientDeviceList.clear();
		DetachedCriteria query = DetachedCriteria.forClass(ClientDevice.class);
//		query.add(Restrictions.eq("depart", departmentService.getDepartmentById(departId)));
//		if(this.getDepartId()!=null)
//		{
//			query.createAlias("depart","depart").add(
//					Restrictions.eq("depart.id",this.getDepartId()));
//		}
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = clientDeviceService.getClientDeviceCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		clientDeviceList.addAll(clientDeviceService.getClientDevices(query, pageSize,
				rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{
		String code =this.clientDeviceService.createDeviceCode();
		clientDevice.setCode(code);
		if (clientId!=null) {
			this.clientName=this.clientService.getClientInfoById(clientId).getClientName();
		}
		if (keshiId!=null) {
			this.keshiName = this.clientKeshiService.getClientKeshiById(keshiId).getName();
		}
		return INPUT;
	}
	
	public String edit() 
	{
		this.clientDevice=this.clientDeviceService.getClientDeviceById(id);
		showInfo();
		return INPUT;
	}
	
	/**
	 * 
	 * @return
	 */
	public String save() throws Exception 
	{
		try 
		{
			if (id == null) 
			{
				clientDeviceService.saveClientDevice(clientDevice,typeId,clientId,keshiId,providerId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} 
			else 
			{
				clientDeviceService.updateClientDevice(clientDevice,typeId,clientId,keshiId,providerId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}
			showInfo();
			return SUCCESS;
		} 
		catch (UniqueConstraintException e) 
		{
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public String delete() throws Exception {
		if (id != null) 
		{	
			clientDeviceService.removeClientDevice(id);
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}			
		return SUCCESS;

	}
	
	
	public String search() throws Exception 
	{
		clientDeviceList.clear();
		DetachedCriteria query = DetachedCriteria.forClass(ClientDevice.class);
		if(!StringTools.isNullOrSpace(this.qCode)){
			query.add(Restrictions.like("code", "%"+this.qCode+"%"));
		}
		if(!StringTools.isNullOrSpace(this.qName)){
			query.add(Restrictions.like("name", "%"+this.qName+"%"));
		}
		if(!StringTools.isNullOrSpace(this.qClient)){
			query.createAlias("type","type").add(
					Restrictions.like("type.type","%"+this.qType+"%"));
		}
		if(!StringTools.isNullOrSpace(this.qClient)){
			query.createAlias("client","client").add(
					Restrictions.like("client.clientName","%"+this.qClient+"%"));
		}
		if(!StringTools.isNullOrSpace(this.qProvider)){
			query.createAlias("provider","provider").add(
					Restrictions.eq("provider.name","%"+this.qProvider+"%"));
		}
		if(!StringTools.isNullOrSpace(this.qServicer)){
			query.add(Restrictions.like("serviceName", "%"+this.qServicer+"%"));
		}
		if(this.qStart != null && this.qEnd != null){
			query.add(Restrictions.between("purchaseDate", qStart , qEnd ));
		}
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = clientDeviceService.getClientDeviceCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.setProjection(null);
		clientDeviceList.addAll(clientDeviceService.getClientDevices(query, pageSize,
				rowStartEnd[0]));
		return SUCCESS;
	}
	
	public String select() 
	{
		clientDeviceList.addAll(clientDeviceService.getClientDevices());
		return SUCCESS;
	}

	public ClientService getClientService() {
		return clientService;
	}

	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	public ClientKeshiService getClientKeshiService() {
		return clientKeshiService;
	}

	public void setClientKeshiService(ClientKeshiService clientKeshiService) {
		this.clientKeshiService = clientKeshiService;
	}

	
}
